﻿// Decompiled with JetBrains decompiler
// Type: Netick.NetworkTimer
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;

#nullable disable
namespace Netick;

/// <summary>
/// A helper struct that can be used to as a timer for various gameplay purposes.
/// </summary>
[Serializable]
public struct NetworkTimer : IEquatable<NetworkTimer>
{
  private int _targetTick;

  public int TargetTick => this._targetTick;

  internal NetworkTimer(int rawValue)
    : this()
  {
    this._targetTick = rawValue;
  }

  public float GetRemainingTime(NetickEngine engine, bool usePredictedTiming = false)
  {
    int num1 = usePredictedTiming ? engine.Tick.Value : engine.AuthoritativeTick.Value;
    int num2 = this._targetTick == 0 ? 0 : (this._targetTick > num1 ? 1 : 0);
    float num3 = Math.Max(0.0f, (float) (this.TargetTick - num1)) * engine.FixedDeltaTime;
    return num2 != 0 ? num3 : 0.0f;
  }

  public float GetElapsedTime(NetickEngine engine, bool usePredictedTiming = false)
  {
    return Math.Max(0.0f, (float) ((usePredictedTiming ? engine.Tick.Value : engine.AuthoritativeTick.Value) - this.TargetTick)) * engine.FixedDeltaTime;
  }

  public bool IsStopped(NetickEngine engine, bool usePredictedTiming = false)
  {
    return !this.IsRunning(engine, usePredictedTiming);
  }

  public bool IsRunning(NetickEngine engine, bool usePredictedTiming = false)
  {
    int num = usePredictedTiming ? engine.Tick.Value : engine.AuthoritativeTick.Value;
    return this._targetTick != 0 && this._targetTick > num;
  }

  public static NetworkTimer StartTimer(float time, NetickEngine engine, bool usePredictedTiming = false)
  {
    return new NetworkTimer((usePredictedTiming ? engine.Tick.Value : engine.AuthoritativeTick.Value) + (int) ((double) time / (double) engine.FixedDeltaTime));
  }

  public override int GetHashCode() => this._targetTick;

  public override bool Equals(object obj)
  {
    return obj is NetworkTimer networkTimer && networkTimer._targetTick == this._targetTick;
  }

  public bool Equals(NetworkTimer other) => this._targetTick == other._targetTick;

  public override string ToString() => this._targetTick.ToString();
}
